package com.zxy.controlcenter.core.log.factory;

import com.xiaoleilu.hutool.exceptions.ExceptionUtil;
import com.xiaoleilu.hutool.http.HttpException;
import com.zxy.controlcenter.core.utils.ConstantFactory;
import com.zxy.controlcenter.core.http.SpringContextHolder;
import com.zxy.controlcenter.modular.system.model.Dictionary;
import com.zxy.controlcenter.modular.system.model.LoginLog;
import com.zxy.controlcenter.modular.system.model.OperationLog;
import com.zxy.controlcenter.modular.system.repository.LoginLogMapper;
import com.zxy.controlcenter.modular.system.repository.OperationLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.TimerTask;

/**
 * 日志操作任务创建工厂
 *
 * @author fengshuonan
 * @date 2016年12月6日 下午9:18:27
 */
@Slf4j
@Component
@DependsOn("constantFactory")
public class LogTaskFactory {
    private static LoginLogMapper loginLogMapper = SpringContextHolder.getBean(LoginLogMapper.class);
    private static OperationLogMapper operationLogMapper = SpringContextHolder.getBean(OperationLogMapper.class);

    public static TimerTask loginLog(final Long userId, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    Dictionary dictionary = new Dictionary();
                    dictionary.setDictType("LOGTYPE");
                    dictionary.setDictCode("LOGIN");
                    Dictionary logDict = getDict(dictionary);
                    String logName = logDict != null && StringUtils.isNotBlank(logDict.getName()) ? logDict.getName() : "登录成功日志";
                    LoginLog loginLog = LogFactory.createLoginLog(logName, userId, null, ip, "200");
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    log.error("创建登录日志异常!", e);
                }
            }
        };
    }

    public static TimerTask loginLog(final String username, final String msg, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    Dictionary dictionary = new Dictionary();
                    dictionary.setDictType("LOGTYPE");
                    dictionary.setDictCode("LOGIN_FAIL");
                    Dictionary logDict = getDict(dictionary);
                    String logName = logDict != null && StringUtils.isNotBlank(logDict.getName()) ? logDict.getName() : "登录失败日志";
                    LoginLog loginLog = LogFactory.createLoginLog(logName, null, "账号:" + username + "," + msg, ip, "500");
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    log.error("创建登录失败异常!", e);
                }
            }
        };
    }

    public static TimerTask exceptionLog(final Long userId, final Exception exception) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    String msg = ExceptionUtil.getMessage(exception);
                    Dictionary dictionary = new Dictionary();
                    dictionary.setDictType("LOGTYPE");
                    dictionary.setDictCode("EXCEPTION");
                    Dictionary logDict = getDict(dictionary);
                    OperationLog operationLog = LogFactory.createOperationLog(logDict.getId(), userId, "异常日志", null, null, msg, "500");
                    operationLogMapper.insert(operationLog);
                } catch (Exception e) {
                    log.error("创建异常日志异常!", e);
                }
            }
        };
    }

    private static Dictionary getDict(Dictionary dictionary) {
        Dictionary logDict = null;
        List<Dictionary> dictionaryList = ConstantFactory.me().getDicList(dictionary, null);
        if (dictionaryList != null && dictionaryList.size() > 0) {
            logDict = dictionaryList.get(0);
        }
        return logDict;
    }

//    public static TimerTask exitLog(final Long userId, final String ip) {
//        return new TimerTask() {
//            @Override
//            public void run() {
//                LoginLog loginLog = LogFactory.createLoginLog(LogType.EXIT, userId, null, ip);
//                try {
//                    loginLogMapper.insert(loginLog);
//                } catch (Exception e) {
//                    log.error("创建退出日志异常!", e);
//                }
//            }
//        };
//    }

//    public static TimerTask bussinessLog(final Long userId, final String bussinessName, final String clazzName, final String methodName, final String msg) {
//        return new TimerTask() {
//            @Override
//            public void run() {
//                OperationLog operationLog = LogFactory.createOperationLog(
//                        LogType.BUSSINESS, userId, bussinessName, clazzName, methodName, msg, LogSucceed.SUCCESS);
//                try {
//                    operationLogMapper.insert(operationLog);
//                } catch (Exception e) {
//                    log.error("创建业务日志异常!", e);
//                }
//            }
//        };
//    }
//
}
